package cn.mazexal.examapi.services;

import cn.hutool.core.util.IdUtil;
import cn.mazexal.examapi.dataware.UserMapper;
import cn.mazexal.examapi.dataware.UserVipLogMapper;
import cn.mazexal.examapi.models.bo.Credentials;
import cn.mazexal.examapi.models.converter.UserConverter;
import cn.mazexal.examapi.models.db.UserDO;
import cn.mazexal.examapi.models.db.UserVipLogDO;
import cn.mazexal.examapi.models.enums.FromTypeEnum;
import cn.mazexal.examapi.models.enums.PlatformEnum;
import cn.mazexal.examapi.models.redisKeys.RedisTableIdKey;
import cn.mazexal.examapi.models.redisKeys.RedisUserKey;
import cn.mazexal.examapi.models.vo.UserVO;
import cn.mazexal.examapi.utils.DateUtils;
import cn.mazexal.examapi.utils.JWTProvider;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author: Mazexal
 * Date: 2025/5/11
 */
@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RedisService redisService;

    @Resource
    private UserConverter userConverter;

    @Resource
    private UserVipService userVipService;

    private Date expireDate = new Date(3501007027330L);

    @Resource
    private JWTProvider jwtProvider;


    // userId 从 10000 起
    private Long getUserId() {
        String key = RedisTableIdKey.USER_ID;
        long userId = redisService.getIncreaseId(key);
        if (userId < 10000) {
            redisService.set(key, String.valueOf(10000));
            return redisService.getIncreaseId(key);
        }
        return userId;
    }

    // 一键登录只有手机号
    @Transactional(rollbackFor = Exception.class)
    public UserVO userRegisterByMobile(String mobile) {
        Long now = System.currentTimeMillis();
        UserDO userDO = new UserDO();
        userDO.setId(getUserId());
        userDO.setMobile(mobile);
        userDO.setUserName(mobile);
        userDO.setCreateTime(now);
        userDO.setIsUnregister(0);
        userDO.setUnregisterTime(0L);
        userMapper.insert(userDO);
        userVipService.initUserVip(userDO.getId(), now - 1);
        return userConverter.toView(userDO);
    }

    public UserVO userLoginOrRegisterByMobile(String mobile) {
        UserDO userDO = userMapper.getUserByMobile(mobile);
        if (userDO == null) {
            return userRegisterByMobile(mobile);
        }
        return userConverter.toView(userDO);
    }

    public String getUserToken(Long userId, String platform) {
        String userKey = RedisUserKey.getUserKey(userId);
        String res = jwtProvider.encode(new Credentials(userId, new Date(), expireDate));
        // 只截取前30字符串, 节约空间
        String hashValue = jwtProvider.getShortToken(res);
        redisService.hashPut(userKey, platform, hashValue);

        PlatformEnum platformEnum = PlatformEnum.getByValue(platform);
        if (platformEnum.equals(PlatformEnum.IOS) || platformEnum.equals(PlatformEnum.iPadOS)) {
            // 移除Android平台的
            redisService.hashDel(userKey, PlatformEnum.ANDROID.getValue());
            redisService.hashDel(userKey, PlatformEnum.AndroidPad.getValue());
        }
        if (platformEnum.equals(PlatformEnum.ANDROID) || platformEnum.equals(PlatformEnum.AndroidPad)) {
            // 移除iOS平台的
            redisService.hashDel(userKey, PlatformEnum.iPadOS.getValue());
            redisService.hashDel(userKey, PlatformEnum.IOS.getValue());
        }
        return res;
    }


}
